package net.dotool.meituan;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.ArrayList;
import java.util.Map;

import javax.json.JsonObject;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import net.dotool.http.HttpRequest;

public class MeiTuanMain {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		for (int i = 0; i < 33; i++) {
			String url = "http://sq.meituan.com/meishi/pn" + i + "/";
			Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.bmcc.com.cn", 18765));
			HttpRequest request = HttpRequest.get(url).setProxy(proxy).setEncodeUrl(true).body("a=乌海");
			String body = request.execute().body();
			Document conn = Jsoup.parse(body);
			parse(conn);
		}
	}

	private static void parse(Document conn) {
		String data = conn.data();
		int sIdx = data.indexOf("poiInfos") + "poiInfos".length() + 2;
		int eIdx = data.indexOf("comHeader") - 3;
		String json = data.substring(sIdx, eIdx);
		ObjectMapper mapper = new ObjectMapper();
		try {
			ArrayList<Map<String, Object>> list = mapper.readValue(json, ArrayList.class);
			for (Map<String, Object> map : list) {
				Object allCommentNum = map.get("allCommentNum");
				Object title = map.get("title");
				System.out.println(title.toString().split("（")[0] + "," + allCommentNum);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
